package com.lsh.oms.core.model.order;

import com.lsh.base.lang.json.Jsons;
import com.lsh.oms.core.model.base.VenderModel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Map;
import java.util.Optional;

import static com.lsh.base.lang.bean.Beans.getString;

/**
 * 订单明细
 *
 * @author huangdong
 * @date 16/8/1
 */
@Getter
@Setter
@ToString
@NoArgsConstructor
public class OrderDetail extends VenderModel implements Serializable {

    private static final long serialVersionUID = 2136224303460403410L;
    /**
     * ID
     */
    private Long id;
    /**
     * 订单号
     */
    private Long orderCode;

    /**
     * 类型
     */
    private Integer type;

    /**
     * SKU编码
     */
    private Long skuCode;

    /**
     * 商品编码
     */
    private Long goodsCode;

    /**
     * 商品名称
     */
    private String goodsName;

    /**
     * 商品条码
     */
    private String goodsBarcode;

    /**
     * 商品售卖单位
     */
    private BigDecimal goodsSaleUnit;

    /**
     * 商品版本
     */
    private Long goodsVersion;

    /**
     * 商品其它属性
     */
    private String goodsExtAttrs;

    /**
     * 原价
     */
    private BigDecimal originalPrice;

    /**
     * 售价
     */
    private BigDecimal sellingPrice;

    /**
     * 单价
     */
    private BigDecimal price;

    /**
     * 数量
     */
    private BigDecimal quantity;

    /**
     * 套餐编号
     */
    private Long packageCode;

    /**
     * ext扩招表信息
     */
    private String detailExt;

    /**
     * 赠品来源商品编码
     */
    private Long giftSrcCode;

    /*
     * 创建缺交数量
     */
    private BigDecimal createLessDelivery;

    /*
     * 发货缺交数量
     */
    private BigDecimal sendLessDelivery;

    /*
     * 创建缺交价格
     */
    private BigDecimal createDeliveryPrice;

    /*
     * 发货缺交价格
     */
    private BigDecimal sendDeliveryPrice;

    /*
     * 签收缺交数量
     */
    private BigDecimal signLessDelivery;

    /*
     * 签收缺交价格
     */
    private BigDecimal signDeliveryPrice;

    /**
     * 创建时间
     */
    private Integer createTime;

    /**
     * 更新时间
     */
    private Integer updateTime;
    /**
     * 行项目
     */
    private Integer itemCode;

    private OrderDetailExt ext;

    private Map<String, Object> extendedProperties;

    private Map<String, Object> orderPackageInfo;

    public BigDecimal getRealQty(){

        return this.getQuantity().multiply(this.getGoodsSaleUnit());
    }

    public Map<String, Object> extendedProperties() {
        if (extendedProperties == null) {
            extendedProperties = Jsons.fromJsonString(detailExt);
        }
        return extendedProperties;
    }

    public Map<String, Object> orderPackageInfo() {
        if (orderPackageInfo == null && packageCode > 0) {
            orderPackageInfo = Jsons.fromJsonString(getString(extendedProperties(), "orderPackageInfo"));
        }
        return orderPackageInfo;
    }

    public Boolean isWeighingGoods() {
        return Integer.valueOf(1).equals(extendedProperties().get("isWeighingGoods")) || "1".equals(extendedProperties().get("isWeighingGoods"));
    }

    public String packageName() {
        return getString(orderPackageInfo(), "sku_info.name");
    }
}
